package com.example.springtest1;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user")
public class RequestController {
    @RequestMapping(value = "/hello",method = RequestMethod.POST)
    public String request(){
        return "你好 Spring Boot";
    }
    //参数传递练习
    @RequestMapping("/r1")
    public String request(String name){
        return "接收到的name参数是："+name;
    }
    //如果参数不匹配
    @RequestMapping("/r2")
    public String request2(Integer age){
        return "接收到的参数age："+age;
    }
    //多个参数的传递
    @RequestMapping("/r3")
    public String request3(@RequestParam("userName") String name, @RequestParam("userAge") Integer age){
        return "姓名："+name+"年龄"+age;
    }
    //数组的传递
    @RequestMapping("/r4")
    public String request4(String[] array){
        return Arrays.toString(array);
    }//这里也可以使用逗号进行分开的操作

    @RequestMapping("/r5")
    public String request5(@RequestParam("getList") List<String> list){
        return "这里接收到的参数list："+list;
    }

   //传递对象
    @RequestMapping("r6")
    public String request6(Student student){
        return student.toString();
    }
    //传递json对象
    @RequestMapping("r7")
    public String request7(@RequestBody Student student){
        return student.toString();
    }

    //这里URL中参数@PathVariable
    @RequestMapping("/r8/{name}/{age}")
    public String request8(@PathVariable String name,@PathVariable("age") Integer userAge){
        return "name："+name+"age："+userAge;
    }
    //文件传输
    @RequestMapping("/r9")
    public String request9(@RequestPart("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("E:/"+file.getOriginalFilename()));
        return "上传的文件的名称："+fileName;
    }

    //传统方式获取cookie
    @RequestMapping("r10")
    public String request10(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            Arrays.stream(cookies).forEach(cookie -> {
                System.out.println(cookie.getName()+"："+cookie.getValue());
            });
        }
        return "获取到cookie";
    }
    //使用注解的方法
    @RequestMapping("r11")
    public String request11(@CookieValue("nihao") String nihao){
        return "nihao的cookie值："+nihao;
    }

    //拿到这里的session
    @RequestMapping("r12")
    public String request(HttpServletRequest request){
         HttpSession session= request.getSession();
         String name=(String) session.getAttribute("name");
         return "获取到了session:"+name;
    }
    //进行session的输入
    @RequestMapping("r13")
    public String request13(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("name","zhangsan");
        return "session设置成功";
    }
}
